package com.duguo.dynamicmvc.factory.processor.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javassist.CtClass;
import javassist.CtMethod;

import com.duguo.dynamicmvc.controller.annotation.ControllerName;
import com.duguo.dynamicmvc.controller.annotation.Interceptor;
import com.duguo.dynamicmvc.factory.ControllerBuilder;
import com.duguo.dynamicmvc.factory.processor.controller.ControllerNameBuilder;
import com.duguo.dynamicmvc.view.annotation.InputView;
import com.duguo.dynamicmvc.view.annotation.SuccessView;

public class ViewAnnotationReaderImpl implements ViewAnnotationReader{
	
	ControllerNameBuilder _controllerNameBuilder;


	public String getInputViewName(CtMethod controllerCtMethod) throws Exception{
		for(Object obj:controllerCtMethod.getAnnotations()){
			if(obj instanceof InputView){
				InputView inputView=(InputView)obj;
				return inputView.value();
			}
		}
		return null;
	}

	public List<String> getInterceptors(CtMethod controllerCtMethod) throws Exception{
		List<String> interceptors=new ArrayList<String>();
		if(controllerCtMethod!=null){
			for(Object obj:controllerCtMethod.getAnnotations()){
				if(obj instanceof Interceptor){
					Interceptor interceptor=(Interceptor)obj;
					Collections.addAll(interceptors, interceptor.value().split(","));
					break;
				}
			}
		}
		return interceptors;
	}


	public boolean hasView(CtClass controllerCtClass,String viewName,ControllerBuilder controllerBuilder) throws Exception{
		List<CtMethod> methodControllers=controllerBuilder.getProcessorHolder().getControllerProcessor().getControllerMethods(controllerCtClass);
		for(CtMethod controllerCtMethod:methodControllers){
			String currentViewName=getControllerName(controllerCtMethod);
			if(viewName.equals(currentViewName)){
				return true;
			}
		}
		return false;
	}

	public String getSuccessViewName(CtMethod controllerCtMethod) throws Exception{
		for(Object obj:controllerCtMethod.getAnnotations()){
			if(obj instanceof SuccessView){
				SuccessView successView=(SuccessView)obj;
				return successView.value();
			}
		}
		return getControllerName(controllerCtMethod);
	}

	public boolean isSuccessViewRedirect(CtMethod controllerCtMethod) throws Exception{
		boolean isRediect=_controllerNameBuilder.isDo("/"+controllerCtMethod.getName());
		if(!isRediect){
			for(Object obj:controllerCtMethod.getAnnotations()){
				if(obj instanceof SuccessView){
					SuccessView successView=(SuccessView)obj;
					return successView.redirect();
				}
			}
		}
		return isRediect;
	}

	

	public String getControllerName(CtMethod controllerCtMethod) throws Exception{
		for(Object obj:controllerCtMethod.getAnnotations()){
			if(obj instanceof ControllerName){
				ControllerName controllerName=(ControllerName)obj;
				return controllerName.value();
			}
		}
		if("notfound".equals(controllerCtMethod.getName())){
			return "404";
		}
		if("handleException".equals(controllerCtMethod.getName())){
			return "500";
		}
		return controllerCtMethod.getName();
	}
	
	public void setControllerNameBuilder(ControllerNameBuilder controllerNameBuilder) {
		_controllerNameBuilder = controllerNameBuilder;
	}
}
